package com.example.dsplatform.Controller;

import cn.hutool.core.util.StrUtil;
import com.example.dsplatform.Dao.Mapper.UserMapper;
import com.example.dsplatform.Service.AdminService;
import com.example.dsplatform.Service.EmailService;
import com.example.dsplatform.Service.LoginService;
import com.example.dsplatform.config.AopAspect.HoneyLogs;
import com.example.dsplatform.config.AopAspect.LogType;
import com.example.dsplatform.pojo.Entity.ResponseResult;
import com.example.dsplatform.Service.ipml.UserServiceImpl;
import com.example.dsplatform.pojo.Entity.User;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.example.dsplatform.utls.StringUtil.checkEmail;

/**
 * @author wdx3004
 * @discription
 * 功能如下：
 * 1 实现管理员对数据库的增删改查
 *
 */

@RestController
@ApiOperation(value = "管理员功能接口")
@RequestMapping("/Admin")
public class AdminController {
    @Autowired
    UserServiceImpl userService;
    @Autowired
    UserMapper userMapper;
    @Autowired
    AdminService adminService;
    @Autowired
    private LoginService loginServcie;
    @Autowired
    EmailService emailService;

    /**
     * @param user
     * @discription 管理员登录
     * @return
     */
    @PostMapping(value = "/login")
    @ApiOperation(value = "实现登录功能")
    public ResponseResult login(@RequestBody User user) {


        return loginServcie.login(user);
    }

    /**
     * @return
     */
    @PostMapping("/hello")
    @PreAuthorize("hasAuthority('sys.insert')")
    @ApiOperation(value = "你好世界")
    public String helloController() {
        return "hello";
    }

    /**
     * @param user
     * {
     *     "username":""
     * }
     * @discription 实现按照名称查询功能
     * @return
     */

    @GetMapping("/select")
    @ApiOperation(value = "实现按照名称查询功能")
    @PreAuthorize("hasAuthority('sys.select')")
    public ResponseResult select(@ApiParam("用户参数") @RequestBody User user) {
        return adminService.selectByName(user);
    }
    /**
     * @param
     * @discription 实现按照名称查询功能
     * @return
     */


    @GetMapping("/selectList")
    @PreAuthorize("hasAuthority('sys.select')")
    @ApiOperation(value = "实现按照名称查询功能")
    @HoneyLogs(operation = "查找列表",type = LogType.SELECT)
    public ResponseResult select() {
        return adminService.selecttable();
    }

    @PostMapping("/signup")

    @ApiOperation(value = "实现插入功能用于注册")
    public ResponseResult signup(@ApiParam("用户参数") @RequestBody User user) {
        return adminService.insert(user);
    }

    @DeleteMapping("/delete")
    @PreAuthorize("hasAuthority('sys.delete')")
    @ApiOperation(value = "实现删除功能")
    public ResponseResult deleteUser(@ApiParam("用户参数") @RequestBody User user) {
        return adminService.delete(user);
    }

    /**
     * @param permname
     * @discription 实现按照权限名称查询带有权限的用户功
     * @return
     */

    @PostMapping("/selectByPerm")
    @PreAuthorize("hasAuthority('sys.select')")
    @ApiOperation(value = "实现按照权限名称查询带有权限的用户功能")
    public ResponseResult selectUserByPerm(@ApiParam("权限名称") String permname) {
        if (StrUtil.isBlank(permname)){
            return new ResponseResult<>(401,"参数不能为空");
        }
        else{
            return adminService.selectUserByPerm(permname);
        }

    }

    @PostMapping("/updateByPerm")
    @PreAuthorize("hasAuthority('sys.update')")
    @ApiOperation(value = "实现按照权限名称修改带有权限的用户功能（没做完）")
    public ResponseResult updateUser(@ApiParam("用户参数") @RequestBody User user) {
        return adminService.updateById(user);

    }
    @GetMapping(value = "/selectRole")
    @ApiOperation(value = "查看用户对应角色")
    public ResponseResult selectUser_Role() {
        return adminService.selectUser_Role();
    }

    /**
     * @param user
     * {
     *    "id":""
     * }
     * @discription 实现按照ID查询功能
     * @return
     */

    @PostMapping(value = "/selectbyID")
    @ApiOperation(value = "c")
    public ResponseResult selectByUID( @RequestBody User user) {
        String userid = user.getId();
        if(StrUtil.isBlank(userid)){
            return new ResponseResult<>(401,"参数不能为空");
        }
        return adminService.selectByUid(user);
    }

    /**
     * @discription 实现邮箱验证
     * @param id String
     * @param email String
     * @return
     */
    /*----邮箱验证---*/
    @PostMapping(value = "/email")
    @ApiOperation(value = "实现邮箱验证")

    public ResponseResult sentEmailCode(@RequestParam String id ,@RequestParam  String email) {
        if(checkEmail(email) ){
            return emailService.sentEmailCode(id,email) ;
        }
        else{
            return new ResponseResult(114,"邮箱格式错误");
        }

    }


    /**
     * @param email String
     * @param code String
     * @return
     * @discription  查看用户对应角色
     */
    @PostMapping(value = "/loginByemail")
    @ApiOperation(value = "查看用户对应角色")
    public ResponseResult loginByCode(@RequestParam String email,@RequestParam  String code) {
        return emailService.loginByCode(email,code);

    }


    /**
     * @discription 分页查询端口
     * @param current_Page
     * @param pageSize
     * @return
     */
    @GetMapping("/selectPageList")
    @PreAuthorize("hasAuthority('sys.delete')")
    @ApiOperation(value = "实现按照名称查询功能(分页查询端口)")

    public ResponseResult selectPage(int current_Page,int pageSize) {

        return adminService.selecttablePage(current_Page,pageSize);
    }



}

/*----对于角色表增删改查---*/









/*    @PutMapping("/admin")
    public String update(@RequestBody Student student){
        userMapper.updateById(student);
        return "update success";
    }

    @PatchMapping("/admin")
    public String updateType(@RequestBody Student student){
        userMapper.updateById(student);
        return "update success";
    }*/

/*----对于角色表增删改查---*/


